package com.sunjob.mroresource.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sunjob.common.domain.resource.*;
import com.sunjob.common.result.Result;
import com.sunjob.mroresource.service.AccessoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.lang.System;
import java.util.LinkedList;
import java.util.List;

/**
 * @author: TomHuang
 * @date: 2022/8/22 8:24
 */

@RestController
@Api(tags = "配件管理")
@RequestMapping("/accessory")
public class AccessoryController {

    @Autowired
    private AccessoryService accessoryService;

    @GetMapping("/find/allAccessory/{pageNum}")
    @ResponseBody
    @ApiOperation(value = "查询所有配件")
    public Result<PageInfo> getAllAccessory(@ApiParam(value = "分页页数") @PathVariable(required = false) int pageNum){
        PageHelper.startPage(pageNum,10);
        List<Accessory> accessorys=accessoryService.selectAllAccessory();
        if(accessorys.size()==0){
            return Result.fail("未查找到配件！");
        }
        PageInfo pageInfo=new PageInfo(accessorys);
        return Result.ok(pageInfo,"查询所有配件成功！");
    }

    @GetMapping("/find/accessoryById/{AccessoryId}")
    @ResponseBody
    @ApiOperation(value = "根据配件id查询配件相关信息")
    public Result<Accessory> getAccessoryById(@ApiParam(value = "配件id",required = true) @PathVariable Long AccessoryId){
        Accessory accessory=accessoryService.selectAccessoryById(AccessoryId);
        if(accessory==null){
            return Result.fail("该配件不存在！");
        }
        return Result.ok(accessory,"查询配件成功！");
    }

    @GetMapping("/find/accessoryByName/{AccessoryName}/{pageNum}")
    @ResponseBody
    @ApiOperation(value = "根据配件名称查询配件相关信息")
    public Result<PageInfo> getAccessoryByName(@ApiParam(value = "配件名称",required = true) @PathVariable String AccessoryName,
                                                    @ApiParam(value = "分页页数") @PathVariable(required = false) int pageNum){
        PageHelper.startPage(pageNum,10);
        List<Accessory> accessorys=accessoryService.selectAccessoryByName(AccessoryName);
        if(accessorys.size()==0){
            return Result.fail("该配件不存在！");
        }
        PageInfo pageInfo=new PageInfo(accessorys);
        return Result.ok(pageInfo,"查询配件成功！");
    }

    @GetMapping("/find/allAccessoryCount")
    @ResponseBody
    @ApiOperation(value = "查询所有配件数量")
    public Integer getAllAccessoryCount(){
        int count=accessoryService.selectAccessoryCount();
        return count;
    }

    @PostMapping("/add/accessory/{accessory}")
    @ApiOperation(value = "添加配件")
    public Result<Integer> addAccessory(@ApiParam(value = "配件实体") @RequestBody Accessory accessory){
        int rt=accessoryService.insertAccessory(accessory);
        if(rt==0){
            return Result.fail("新增配件数据失败！");
        }
        return Result.ok(rt,"新增配件数据成功！");
    }

    @PostMapping("/add/species/{accessorySpecies}")
    @ApiOperation(value = "添加配件种类")
    public Result<Integer> addAccessorySpecies(@ApiParam(value = "配件种类") @RequestBody AccessorySpecies accessorySpecies){
        int rt=accessoryService.insertAccessorySpecies(accessorySpecies);
        if(rt==0){
            return Result.fail("新增配件种类数据失败！");
        }
        return Result.ok(rt,"新增配件种类数据成功！");
    }

    @PostMapping("/add/status/{accessoryStatus}")
    @ApiOperation(value = "添加配件状态")
    public Result<Integer> addAccessoryStatus(@ApiParam(value = "配件状态") @RequestBody AccessoryStatus accessoryStatus){
        int rt=accessoryService.insertAccessoryStatus(accessoryStatus);
        if(rt==0){
            return Result.fail("新增配件状态数据失败！");
        }
        return Result.ok(rt,"新增配件状态数据成功！");
    }

    @PostMapping("/add/supplier/{accessorySupplier}")
    @ApiOperation(value = "添加配件供货商")
    public Result<Integer> addAccessorySupplier(@ApiParam(value = "配件供货商") @RequestBody AccessorySupplier accessorySupplier){
        int rt=accessoryService.insertAccessorySupplier(accessorySupplier);
        if(rt==0){
            return Result.fail("新增配件供货商数据失败！");
        }
        return Result.ok(rt,"新增配件供货商数据成功！");
    }

    @PostMapping("/add/tech/{accessoryTech}")
    @ApiOperation(value = "添加配件技术要求")
    public Result<Integer> addAccessoryTech(@ApiParam(value = "配件技术要求") @RequestBody AccessoryTech accessoryTech){
        int rt=accessoryService.insertAccessoryTech(accessoryTech);
        if(rt==0){
            return Result.fail("新增配件技术要求数据失败！");
        }
        return Result.ok(rt,"新增配件技术要求数据成功！");
    }

    @GetMapping("/find/accessorySpecies/{accessoryId}")
    @ResponseBody
    @ApiOperation(value = "根据配件id查询配件种类")
    public Result<String> getAccessorySpeciesById(@ApiParam(value = "配件id",required = true) @PathVariable Long accessoryId){
        String accessorySpecies=accessoryService.selectAccessorySpecies(accessoryId);
        if(accessorySpecies==null){
            return Result.fail("该配件种类不存在！");
        }
        return Result.ok(accessorySpecies,"查询配件种类成功！");
    }

    @GetMapping("/find/allAccessorySpecies/{pageNum}")
    @ResponseBody
    @ApiOperation(value = "查询所有配件种类")
    public Result<PageInfo> getAllAccessorySpecies(@ApiParam(value = "分页页数") @PathVariable(required = false) int pageNum){
        PageHelper.startPage(pageNum,10);
        List<String> accessorySpeciess=accessoryService.selectAllAccessorySpecies();
        if(accessorySpeciess.size()==0){
            return Result.fail("未查询到配件种类！");
        }
        PageInfo pageInfo=new PageInfo(accessorySpeciess);
        return Result.ok(pageInfo,"查询配件种类成功！");
    }

    @GetMapping("/find/accessoryStatus/{accessoryId}")
    @ResponseBody
    @ApiOperation(value = "根据配件id查询配件状态")
    public Result<String> getAccessoryStatusById(@ApiParam(value = "配件id",required = true) @PathVariable Long accessoryId){
        int re=accessoryService.selectAccessoryStatus(accessoryId);
        if(re<0){
            return Result.fail("查询该配件状态失败！");
        }
        String status= (re==0?"正常":"异常");
        return Result.ok(status,"查询配件种类成功！");
    }

    @GetMapping("/find/allAccessoryStatus/{pageNum}")
    @ResponseBody
    @ApiOperation(value = "查询所有配件状态")
    public Result<PageInfo> getAllAccessoryStatus(@ApiParam(value = "分页页数") @PathVariable(required = false) int pageNum){
        PageHelper.startPage(pageNum,10);
        List<Integer> accessoryStatuss=accessoryService.selectAllAccessoryStatus();
        if(accessoryStatuss.size()==0){
            return Result.fail("未查询到配件状态！");
        }
        List<String> str=new LinkedList<>();
        for (int i = 0; i < accessoryStatuss.size(); i++) {
            str.add((accessoryStatuss.get(i)==0?"正常":"异常"));
        }
        PageInfo pageInfo=new PageInfo(str);
        return Result.ok(pageInfo,"查询所有配件状态成功！");
    }

    @GetMapping("/find/accessorySupplier/{accessoryId}")
    @ResponseBody
    @ApiOperation(value = "根据配件id查询配件供货商管理")
    public Result<String> getAccessorySupplier(@ApiParam(value = "配件id",required = true) @PathVariable Long accessoryId){
        String supplier=accessoryService.selectAccessorySupplier(accessoryId);
        if(supplier==null){
            return Result.fail("未查找到该配件供货商管理！");
        }
        return Result.ok(supplier,"查询配件配件供货商管理成功！");
    }

    @GetMapping("/find/allAccessorySupplier/{pageNum}")
    @ResponseBody
    @ApiOperation(value = "查询所有配件供货商")
    public Result<PageInfo> getAllAccessorySupplier(@ApiParam(value = "分页页数") @PathVariable(required = false) int pageNum){
        PageHelper.startPage(pageNum,10);
        List<String> accessorySuppliers=accessoryService.selectAllAccessorySupplier();
        if(accessorySuppliers.size()==0){
            return Result.fail("未查询到配件供货商！");
        }
        PageInfo pageInfo=new PageInfo(accessorySuppliers);
        return Result.ok(pageInfo,"查询所有配件供货商成功！");
    }

    @GetMapping("/find/accessoryTech/{accessoryId}")
    @ResponseBody
    @ApiOperation(value = "根据配件id查询配件技术要求")
    public Result<String> getAccessoryTech(@ApiParam(value = "配件id",required = true) @PathVariable Long accessoryId){
        String tech=accessoryService.selectAccessoryTech(accessoryId);
        if(tech==null){
            return Result.fail("未查找到该配件技术要求！");
        }
        return Result.ok(tech,"查询配件技术要求成功！");
    }

    @GetMapping("/find/allAccessoryTech/{pageNum}")
    @ResponseBody
    @ApiOperation(value = "查询所有配件技术要求")
    public Result<PageInfo> getAllAccessoryTech(@ApiParam(value = "分页页数") @PathVariable(required = false) int pageNum){
        PageHelper.startPage(pageNum,10);
        List<String> accessoryTechs=accessoryService.selectAllAccessoryTech();
        if(accessoryTechs.size()==0){
            return Result.fail("未查询到配件技术要求！");
        }
        PageInfo pageInfo=new PageInfo(accessoryTechs);
        return Result.ok(pageInfo,"查询所有配件技术要求成功！");
    }

    @PostMapping("edit/accessory/{accessory}")
    @ApiOperation(value = "修改配件相关信息")
    public Result<Integer> editAccessory(@ApiParam(value = "配件实体") @RequestBody Accessory accessory){
        int rt=accessoryService.updateAccessory(accessory);
        if(rt==0){
            return Result.fail("修改配件数据失败！");
        }
        return Result.ok(rt,"修改配件数据成功！");
    }

    @PostMapping("edit/species/{accessorySpecies}")
    @ApiOperation(value = "修改配件种类")
    public Result<Integer> editAccessorySpecies(@ApiParam(value = "配件种类实体") @RequestBody AccessorySpecies accessorySpecies){
        int rt=accessoryService.updateAccessorySpecies(accessorySpecies);
        if(rt==0){
            return Result.fail("修改配件种类数据失败！");
        }
        return Result.ok(rt,"修改配件种类数据成功！");
    }

    @PostMapping("edit/supplier/{accessorySupplier}")
    @ApiOperation(value = "修改配件供货商管理")
    public Result<Integer> editAccessorySupplier(@ApiParam(value = "配件供货商管理实体") @RequestBody AccessorySupplier accessorySupplier){
        int rt=accessoryService.updateAccessorySupplier(accessorySupplier);
        if(rt==0){
            return Result.fail("修改配件供货商管理数据失败！");
        }
        return Result.ok(rt,"修改配件供货商管理数据成功！");
    }

    @PostMapping("edit/tech/{accessoryTech}")
    @ApiOperation(value = "修改配件技术要求")
    public Result<Integer> editAccessoryTech(@ApiParam(value = "配件技术要求实体") @RequestBody AccessoryTech accessoryTech){
        int rt=accessoryService.updateAccessoryTech(accessoryTech);
        if(rt==0){
            return Result.fail("修改配件技术要求数据失败！");
        }
        return Result.ok(rt,"修改配件技术要求数据成功！");
    }

    @GetMapping("/find/vehicletype/{accessoryId}")
    @ResponseBody
    @ApiOperation(value = "根据配件id查询配件适用车辆")
    public Result<String> getVehicletype(@ApiParam(value = "配件id",required = true) @PathVariable Long accessoryId){
        String type=accessoryService.selectAccessoryVehicleType(accessoryId);
        if(type==null){
            return Result.fail("未查找到该配件适用车辆！");
        }
        return Result.ok(type,"查询配件配件适用车辆成功！");
    }

    @GetMapping("/find/system/{accessoryId}")
    @ResponseBody
    @ApiOperation(value = "根据配件id查询配件所属专业系统")
    public Result<String> getSystem(@ApiParam(value = "配件id",required = true) @PathVariable Long accessoryId){
        String system=accessoryService.selectAccessorySystem(accessoryId);
        if(system==null){
            return Result.fail("未查找到该配件所属专业系统！");
        }
        return Result.ok(system,"查询该配件所属专业系统成功！");
    }

    @GetMapping("/find/allAccessoryInorout/{accessoryId}/{pageNum}")
    @ResponseBody
    @ApiOperation(value = "根据配件id查询配件所有出入库记录")
    public Result<PageInfo> getAllAccessoryInorout(@ApiParam(value = "配件id",required = true) @PathVariable Long accessoryId,
                                                        @ApiParam(value = "分页页数") @PathVariable(required = false) int pageNum){
        PageHelper.startPage(pageNum,10);
        List<Inorout> Inorouts=accessoryService.selectInoroutByAccessoryId(accessoryId);
        if(Inorouts.size()==0){
            return Result.fail("未查找到该配件出入库记录！");
        }
        /*for(int i=0;i<Inorouts.size();i++){
            Inorouts.get(i).setInoroutStatus(Inorouts.get(i).getInoroutStatus()==0?"入库":"出库");
        }*/
        PageInfo pageInfo=new PageInfo(Inorouts);
        return Result.ok(pageInfo,"查询配件出入库记录成功！");
    }

    @PostMapping("/add/accessoryInorout/{inorout}")
    @ApiOperation(value = "添加配件出入库记录")
    public Result<Integer> addAccessoryInorout(@ApiParam(value = "配件出入库记录实体") @RequestBody Inorout inorout){
        Accessory accessory=accessoryService.selectAccessoryById(inorout.getTargetId());
        Double price=accessory.getAccessoryPrice();
        int count=inorout.getInoroutNumber();
        inorout.setTotalPrice(count*price);
        int status=inorout.getInoroutStatus();
        int nowCount;
        //校验库存数据
        if(status==1){
            if(accessory.getAccessoryStock()<count){
                return Result.fail("该配件库存不足以出库！");
            }else {
                nowCount=accessory.getAccessoryStock()-count;
            }
        }else {
            nowCount=accessory.getAccessoryStock()+count;
        }
        System.out.println(nowCount);
        accessoryService.updateAccessoryCount(inorout.getTargetId(),nowCount);
        int rt=accessoryService.insertAccessoryInorout(inorout);
        if(rt==0){
            return Result.fail("新增配件出入库记录数据失败！");
        }
        return Result.ok(rt,"新增配件出入库记录数据成功！");
    }


}
